﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using PowerSong.SongDatabase;

    public partial class VerseListBox : UserControl
        {

        public delegate void SongChangeEventArgs(Song song);

        public delegate void VerseChangeEventArgs(Song song, int verseIndex);

        public event VerseChangeEventArgs SelectedVerseChanged;
        
        public event VerseChangeEventArgs ActiveVerseChanged;

        public event SongChangeEventArgs ActiveSongChanged;

        private int selectedVerseIndex = -1;

        public int SelectedVerseIndex 
            {
            get
                {
                return selectedVerseIndex;
                }
            private set
                {
                var oldValue = this.selectedVerseIndex;
                this.selectedVerseIndex = value;
                if (oldValue != value && SelectedVerseChanged != null) SelectedVerseChanged(activeSong, value);
                }
            }

        private int activeVerseIndex = -1;

        public int ActiveVerseIndex
            {
            get
                {
                return activeVerseIndex;
                }
            private set
                {
                var oldValue = this.activeVerseIndex;
                this.activeVerseIndex = value;
                if (oldValue != value && ActiveVerseChanged != null) ActiveVerseChanged(activeSong, value);
                }
            }

        private Song activeSong = null;

        public Song ActiveSong 
            {
            get
                {
                return activeSong;
                }
            private set
                {
                var oldValue = this.activeSong;
                this.activeSong = value;
                if (oldValue != value && ActiveSongChanged != null) ActiveSongChanged(activeSong);
                }
            }

        private StackPanel versesStackPanel = null;

        public VerseListBox()
            {

            // Create a border
            var border = new Border();
            border.BorderThickness = new Thickness(1);
            border.BorderBrush = Brushes.Gray;

            // Create a scroll viewer
            var viewer = new ScrollViewer();
            viewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
            viewer.MouseDoubleClick += ScrollViewer_MouseDoubleClick;
            border.Child = viewer;

            // Create a stack panel
            this.versesStackPanel = new StackPanel();
            versesStackPanel.Orientation = Orientation.Vertical;
            viewer.Content = versesStackPanel;

            this.AddChild(border);

            }

        public void ChangeSong(Song newSong)
            {

            this.ActiveSong = newSong;

            versesStackPanel.Children.Clear();

            if (newSong != null)
                {
                foreach (var verseText in newSong.Verses)
                    {

                    var textBlock = new TextBlock();
                    textBlock.Text = verseText;
                    textBlock.FontSize += 1;
                    textBlock.TextWrapping = TextWrapping.Wrap;

                    var border = new Border();
                    border.MouseDown += VerseMouseDown;
                    border.Margin = new Thickness(2, 0, 2, 0);
                    border.Padding = new Thickness(3);
                    border.BorderBrush = Brushes.Silver;
                    border.BorderThickness = new Thickness(0, 0, 0, 1);
                    border.Child = textBlock;

                    versesStackPanel.Children.Add(border);

                    }
                }

            this.SelectedVerseIndex = 0;
            this.ActiveVerseIndex = 0;
            UpdateControls();

            }

        public void ChangeVerse(int verseIndex)
            {

            // Set the active and selected verses
            this.ActiveVerseIndex = verseIndex;
            this.SelectedVerseIndex = (verseIndex + 1) % activeSong.Verses.Count;
            UpdateControls();

            }

        private void VerseMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
            var border = (Border)sender;
            var control = (TextBlock)border.Child;
            this.SelectedVerseIndex = versesStackPanel.Children.IndexOf(border);
            UpdateControls();
            }

        private void UpdateControls()
            {
            foreach (Border border in versesStackPanel.Children)
                {
                var control = (TextBlock)border.Child;
                border.Background = SystemColors.MenuBrush;
                control.Foreground = SystemColors.MenuTextBrush;
                var index = versesStackPanel.Children.IndexOf(border);
                if (SelectedVerseIndex == index)
                    {
                    border.Background = SystemColors.MenuHighlightBrush;
                    control.Foreground = SystemColors.HighlightTextBrush;
                    }
                else if (ActiveVerseIndex == index)
                    {
                    border.Background = Brushes.Black;
                    control.Foreground = Brushes.White;
                    }
                }
            }

        private void ScrollViewer_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
            ChangeVerse(this.SelectedVerseIndex);
            }

        }

    }
